package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.dto.responsebody.CollectFolderResponseBody;
import com.yuesystem.entity.CollectFolder;
import com.yuesystem.entity.CollectItem;
import com.yuesystem.mapper.CollectFolderMapper;
import com.yuesystem.service.CollectFolderService;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 收藏夹接口实现类.
 */
@Service
@Slf4j
public class CollectFolderServiceImpl
    extends ServiceImpl<CollectFolderMapper, CollectFolder> implements CollectFolderService {
  @Autowired
  CollectItemServiceImpl collectItemService;

  /**
   * 获取某个用户的收藏夹列表.
   *
   * @return 收藏夹列表
   */
  @Override
  public List<CollectFolderResponseBody> getUserFolder() {
    Long userId = CollectItemServiceImpl.getUserId();
    LambdaQueryWrapper<CollectFolder> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectFolder::getUserId, userId);
    List<CollectFolder> folders = list(queryWrapper);
    List<CollectFolderResponseBody> list = new ArrayList<>();
    for (CollectFolder folder : folders) {
      CollectFolderResponseBody body = new CollectFolderResponseBody();
      body.setFolderId(folder.getId());
      body.setFolderName(folder.getFolderName());
      body.setNum(this.getItemNum(folder.getId()));
      if (null == folder.getDescription()) {
        body.setDescription("暂无内容");
      } else {
        body.setDescription(folder.getDescription());
      }
      list.add(body);
    }
    return list;
  }

  /**
   * 删除文件夹.
   *
   * @param folderId 收藏夹id
   * @return 操作是否成功
   */
  @Override
  public Boolean removeFolder(Long folderId) {
    Long userId = CollectItemServiceImpl.getUserId();
    LambdaQueryWrapper<CollectItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectItem::getUserId, userId)
        .eq(CollectItem::getFolderId, folderId);
    this.removeById(folderId);
    Boolean flag = collectItemService.remove(queryWrapper);
    return flag;
  }

  /**
   * 获取某个收藏夹下的收藏条目数.
   *
   * @param folderId 收藏夹id
   * @return 收藏夹下的收藏条目数
   */
  public int getItemNum(Long folderId) {
    Long currentId = CollectItemServiceImpl.getUserId();
    LambdaQueryWrapper<CollectItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectItem::getUserId, currentId)
        .eq(CollectItem::getFolderId, folderId);
    int result = (int) collectItemService.count(queryWrapper);
    return result;
  }

  /**
   * 用户新建收藏夹.
   *
   * @param folderName 收藏夹名称
   * @param desc       收藏夹描述
   * @return 操作是否成功
   */
  @Override
  public Boolean add(String folderName, String desc) {
    Long currentId = CollectItemServiceImpl.getUserId();
    CollectFolder collectFolder = new CollectFolder();
    collectFolder.setUserId(currentId);
    collectFolder.setFolderName(folderName);
    collectFolder.setDescription(desc);
    boolean save = this.save(collectFolder);
    return save;
  }

  /**
   * 检测当前新建收藏夹是否与已有收藏夹名称重复.
   *
   * @param folderName 用户输入的收藏夹名称
   * @return 是否重复
   */
  @Override
  public Boolean isRepeat(String folderName) {
    List<CollectFolderResponseBody> list = this.getUserFolder();
    for (CollectFolderResponseBody body : list) {
      if (body.getFolderName().equals(folderName)) {
        return true;
      }
    }
    return false;
  }
}
